

package kr.ac.ssu.yoobh17.mPlayer.musicService;


import java.util.ArrayList;
import java.util.Random;

import kr.ac.ssu.yoobh17.mPlayer.GlobalClass;


public class MusicListHelper {

	private boolean				suffleMode			= false;
	private int					loopingMode			= GlobalClass.LOOPING_FALSE;

	private ArrayList<String>	originalPlayingList	= null;
	private ArrayList<String>	sufflePlayingList	= null;

	private int					nowPlayingPosition	= 0;

	//---------------------------------------------------------------------------------------------------

	public MusicListHelper( boolean suffleMode, int loopingMode ){
		
		this.suffleMode		= suffleMode;
		this.loopingMode 	= loopingMode;
		
	}

	//---------------------------------------------------------------------------------------------------
	
	public void setPlayList( ArrayList<String> playList, int position ){
		
		originalPlayingList = playList;

		if( suffleMode ){
			
			generateSuffleList();
			
		}
		else{
			
			nowPlayingPosition = position;
			
		}
	}

	public void setSuffleMode( final boolean suffleMode ){
		
		if( suffleMode == this.suffleMode ){
			
			return;
		}
		else{

			this.suffleMode = suffleMode;

			if ( this.suffleMode ) {

				generateSuffleList();

			}
			else{

				nowPlayingPosition	= originalPlayingList.indexOf( sufflePlayingList.get( nowPlayingPosition ) );
				sufflePlayingList	= null;

			}
		}
	}

	private void generateSuffleList() {
		
		ArrayList<String> 	tempList 	= new ArrayList<String>();
		Random 				random 		= new Random();
		
		for( int i = 0; i < originalPlayingList.size(); i++ ){
			
			tempList.add( originalPlayingList.get( i ) );
			
		}

		sufflePlayingList = new ArrayList<String>();
		
		sufflePlayingList.add( 0, tempList.remove( nowPlayingPosition ) );
		
		for( int i = 0; i < originalPlayingList.size() - 1; i++ ){

			sufflePlayingList.add( tempList.remove( random.nextInt( tempList.size() ) ) );
			
		}
		
		nowPlayingPosition = 0;
		
	}

	//---------------------------------------------------------------------------------------------------

	public void setLoopingMode( int loopingMode ){
		
		this.loopingMode = loopingMode;
				
	}
	
	public void setPreviousMusic(){
		
		ArrayList<String> tempList = ( suffleMode )? ( sufflePlayingList ): ( originalPlayingList );

		if( 0 <= nowPlayingPosition - 1 ){
			
			nowPlayingPosition--;
			
		}
		else{
			
			nowPlayingPosition = tempList.size() - 1;
			
		}
	}
	
	public String setNextMusic( boolean framUser ){
		
		ArrayList<String> tempList = ( suffleMode )? ( sufflePlayingList ): ( originalPlayingList );

		if ( framUser || GlobalClass.LOOPING_TRUE == loopingMode ) {

			if ( tempList.size() > nowPlayingPosition + 1 ) {

				nowPlayingPosition++;

			}
			else {

				nowPlayingPosition = 0;

			}
		}
		else if( GlobalClass.LOOPING_FALSE == loopingMode ){

			if ( tempList.size() > nowPlayingPosition + 1 ) {

				nowPlayingPosition++;

			}
			else {

				return "-1";
			}
		}

		return getPlayingMusicId();
	}
	
	//---------------------------------------------------------------------------------------------------

	public String getPlayingMusicId(){

		ArrayList<String> tempList = ( suffleMode )? ( sufflePlayingList ): ( originalPlayingList );

		return ( null == tempList )? ( "-1" ): ( tempList.get( nowPlayingPosition ) );
	}

	//---------------------------------------------------------------------------------------------------

	public void setNowPlayingPosition( int nowPlayingPosition ) {
	
		this.nowPlayingPosition = nowPlayingPosition;
	}

	//---------------------------------------------------------------------------------------------------

	public int getNowPlayingPosition(){
		
		return nowPlayingPosition;
	}
	
	public boolean getSuffleMode(){
		
		return suffleMode;
	}

	public int getLoopingMode(){
		
		return loopingMode;
	}
	
	public int getPlayingListSize(){

		ArrayList<String> tempList = ( suffleMode )? ( sufflePlayingList ): ( originalPlayingList );

		return tempList.size();
	}

}
